
package com.kugou.game.sdk.ui.dialog;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.utils.AppUtil;
import com.kugou.game.sdk.utils.Rx;

/**
 * 选择用户对话框
 * 
 * @author chenys
 */
public class AllUserDialog extends BaseSDKDialog {

    private String mCurrentUser = null;

    private ListView mListView;

    private ArrayList<User> mUsers = new ArrayList<User>();

    private LayoutInflater mInflater;

    private OnSelectUserListener mOnUserSelectListener;

    private OnDeleteUserListener mNoSelectListener;

    private Context mContext = null;

    private UserAdapter mAdapter = null;

    public Button but;

    private boolean mEditFlag = false;// true:表示不显示，false:表示显示

    public AllUserDialog(Context context) {
        super(context);
        this.mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }

    public void setCurrentUser(String currentUser) {
        this.mCurrentUser = currentUser;
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();
        setOKBtnVisibility(false);
        setCancelBtnVisibility(false);
        setCanceledOnTouchOutside(true);
        setCancelable(true);
        but = (Button) findViewById(Rx.id.kg_all_user_but);
        mListView = (ListView) findViewById(Rx.id.kg_all_user_list);
        if (mListView == null) {
            throw new RuntimeException(
                    "Your content must have a ImageButton whose id attribute is "
                            + "'R.id.kg_all_user_list'");
        }
        but.setOnClickListener(mEditListener);
        mAdapter = new UserAdapter();
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mOnUserSelectListener != null) {
                    mOnUserSelectListener.afterSelectUser(mUsers.get(position));
                    dismiss();
                }
            }
        });

        setDialogSize();
    }

    private void setDialogSize() {
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics dm = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
        int parentWidth = dm.widthPixels;
        int parentHeight = dm.heightPixels;

        if (parentWidth < parentHeight) {// 竖屏
            lp.width = (int) (parentWidth * 0.9);
            lp.height = (int) (lp.width * 0.84);
        } else {
            lp.width = (int) (parentHeight * 0.9);
            lp.height = (int) (lp.width * 0.84);
        }

        dialogWindow.setAttributes(lp);
    }

    public void notifyAdapterChage() {
        mAdapter.notifyDataSetInvalidated();
    }

    public void addDatas(ArrayList<User> users) {
        if (users != null && users.size() > 0) {
            mUsers.addAll(users);
        }
    }

    public void clearDatas() {
        if (mUsers != null) {
            mUsers.clear();
        }
    }

    public void setOnSelectUserListener(OnSelectUserListener listener) {
        this.mOnUserSelectListener = listener;
    }

    public void setOnDeleteUserListener(OnDeleteUserListener listener) {
        this.mNoSelectListener = listener;
    }

    class UserAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mUsers.size();
        }

        @Override
        public Object getItem(int position) {
            return mUsers.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyClass cache;
            if (convertView == null) {
                convertView = mInflater.inflate(Rx.layout.kg_all_user_item, null);
                cache = new MyClass();
                cache.text = (TextView) convertView.findViewById(Rx.id.kg_all_user_text);
                cache.delButton = (ImageView) convertView
                        .findViewById(Rx.id.kg_all_user_del_button);
                cache.userMark = (ImageView) convertView.findViewById(Rx.id.kg_all_user_mark);
                convertView.setTag(cache);
            } else {
                cache = (MyClass) convertView.getTag();
            }
            cache.text.setText(mUsers.get(position).getAccountName());
            if (!TextUtils.isEmpty(mCurrentUser)
                    && mUsers.get(position).getAccountName().equals(mCurrentUser)) {
                cache.userMark.setVisibility(View.VISIBLE);
            } else {
                cache.userMark.setVisibility(View.INVISIBLE);
            }
            cache.delButton.setTag(position);
            cache.delButton.setOnClickListener(mClickListener);
            if (mEditFlag) {
                cache.delButton.setVisibility(View.VISIBLE);
                cache.text.setPadding(AppUtil.dip2px(mContext, 0), 0, 0, 0);
            } else {
                cache.delButton.setVisibility(View.GONE);
                cache.text.setPadding(AppUtil.dip2px(mContext, 9), 0, 0, 0);
            }
            return convertView;
        }

        class MyClass {
            TextView text;

            ImageView delButton;

            ImageView userMark;
        }
    }

    /*
     * 设置编辑状态
     */
    public void setEditFlag(Boolean mEditFlag) {
        this.mEditFlag = mEditFlag;
        int drawableId = Rx.drawable.kg_recharge_account_bg_pressed;
        Drawable dw = mContext.getResources().getDrawable(drawableId);
        but.setBackgroundDrawable(dw);
    }

    /**
     * 编辑状态监听
     */
    private View.OnClickListener mEditListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mEditFlag) {
                mEditFlag = false;
                int drawableId = Rx.drawable.kg_recharge_account_bg_pressed;
                Drawable dw = mContext.getResources().getDrawable(drawableId);
                but.setBackgroundDrawable(dw);
                notifyAdapterChage();
            } else {
                mEditFlag = true;
                int drawableId = Rx.drawable.kg_recharge_account_bg;
                Drawable dw = mContext.getResources().getDrawable(drawableId);
                but.setBackgroundDrawable(dw);
                notifyAdapterChage();
            }
        }
    };

    /**
     * 删除按键监听
     */
    private View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Object obj = v.getTag();
            if (obj != null) {
                int pos = (Integer) obj;
                User delUser = mUsers.get(pos);
                UserManager.getInstance().deleteUserData(delUser);
                mUsers.remove(pos);
                mAdapter.notifyDataSetChanged();

                if (mNoSelectListener != null) {
                    if (mUsers.size() == 0) {
                        dismiss();
                        mNoSelectListener.afterDeleteUser(0, delUser.getAccountName());
                    } else {
                        mNoSelectListener.afterDeleteUser(mUsers.size(), delUser.getAccountName());
                    }
                }
            }
        }
    };

    /**
     * 描述:在用户账号选择对话框中选择用户账号
     * 
     * @author liux
     * @since 2013-12-14 下午06:02:02
     */
    public interface OnSelectUserListener {
        /**
         * 在用户账号选择对话框中选择某个用户账号
         * 
         * @param userData
         */
        public void afterSelectUser(User userData);

    }

    /**
     * 描述:删除用户选择列表回调接口
     * 
     * @author liux
     * @since 2013-12-14 下午05:47:21
     */
    public interface OnDeleteUserListener {
        /**
         * 在用户账号选择对话框中删除用户账号
         * 
         * @param remaiinSize 列表中剩余用户账号数目
         * @param deleteNickName 删除的用户名
         */
        public void afterDeleteUser(int remainSize, String deleteNickName);
    }

}
